package com.huopaolan.yohuoshop.Mvp.View.Fragment;


import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;

import com.huopaolan.lib_core.Base.BaseFragment;
import com.huopaolan.yohuoshop.Adapter.ShowAdapter;
import com.huopaolan.yohuoshop.Bean.ShowEntity;
import com.huopaolan.yohuoshop.Di.Compont.DaggerShowCommunityFragmentCompont;
import com.huopaolan.yohuoshop.Di.Modules.ShowCommunityModules;
import com.huopaolan.yohuoshop.Listener.HomeBannerImageLoader;
import com.huopaolan.yohuoshop.Mvp.Contract.CommunityFragmentContract;
import com.huopaolan.yohuoshop.Mvp.Presenter.ShowFragmentPresenter;
import com.huopaolan.yohuoshop.NetWork.API;
import com.huopaolan.yohuoshop.R;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ShowFragment extends BaseFragment<ShowFragmentPresenter> implements CommunityFragmentContract.CommunityShowIView, BaseRefreshListener {
    //banner集合
    List<ShowEntity.Banner> bannerList = new ArrayList<>();
    //brand集合
//    List<ShowEntity.Brand> brandList = new ArrayList<>();
    //values集合
    List<ShowEntity.Values> valuesList = new ArrayList<>();
    private View bannerView;
    private Banner banner;
    private PullToRefreshLayout pullToRefreshLayout;
    private ShowAdapter showAdapter;
    private RecyclerView recyclerView;

    @Override
    public View createView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_show, null);
    }

    @Override
    public void onStart() {
        Window window = getActivity().getWindow();
        window.setStatusBarColor(Color.BLACK);
        super.onStart();
    }

    @Override
    public void initView(View view) {
        //初始化
        pullToRefreshLayout = view.findViewById(R.id.show_pull);
        pullToRefreshLayout.setRefreshListener(this);
        recyclerView = view.findViewById(R.id.show_recycler);
        //配置头布局
        bannerView = View.inflate(getActivity(), R.layout.item_stroll_banner, null);
        banner = bannerView.findViewById(R.id.stroll_banner);
        //配置适配器
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        showAdapter = new ShowAdapter(R.layout.item_show_community);
        showAdapter.addHeaderView(bannerView, 0);
        recyclerView.setAdapter(showAdapter);
    }

    @Override
    public void initData() {
        p.ShowRequest();
    }

    @Override
    public void setUpDagger() {
        DaggerShowCommunityFragmentCompont.builder()
                .showCommunityModules(new ShowCommunityModules(this))
                .build().inject(this);
    }

    @Override
    public void CommunityShowSuccess(Map<String, List> entity) {
        bannerList.addAll(entity.get("bannerList"));
//        brandList.addAll(entity.get("brandList"));
        valuesList.addAll(entity.get("valuesList"));
        showAdapter.addData(valuesList);
        List<String> banners = new ArrayList<>();
        for (int i = 0; i < bannerList.size(); i++) {
            banners.add(API.BaseUrl + bannerList.get(i).recommend_url);
        }
        banner.setImages(banners);
        banner.isAutoPlay(true);
        banner.setIndicatorGravity(BannerConfig.CENTER);
        banner.setDelayTime(1 * 1500);
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        banner.setImageLoader(new HomeBannerImageLoader());
        banner.start();
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    @Override
    public void refresh() {
        showAdapter.getData().clear();
        p.ShowRequest();
        pullToRefreshLayout.finishRefresh();
    }

    @Override
    public void loadMore() {
        p.ShowRequest();
        pullToRefreshLayout.finishRefresh();
    }
}
